package com.lai.hospital.handler;

import com.alibaba.fastjson.JSON;
import com.lai.hospital.vo.Result;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.lai.hospital.constant.CommonConst.APPLICATION_JSON;

/**
 * @author : lai
 * @version V1.0
 * @Project: hospital-springboot
 * @Package com.lai.hospital.handler
 * @Description: 登录失败处理器
 * @date Date : 2022年11月17日 11:34
 */
@Component
public class AuthenticationFailHandlerImpl implements AuthenticationFailureHandler {
    /**
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param e 授权异常
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setContentType(APPLICATION_JSON);
        httpServletResponse.getWriter().write(JSON.toJSONString(Result.fail(e.getMessage())));
    }
}
